package com.shijie.newvideo.base;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * @author zhangdunxiang
 */

public class BaseRecyclerViewHolder<Bean> extends RecyclerView.ViewHolder {

    private OnItemClickListener mOnItemClickListener;

    public BaseRecyclerViewHolder(View itemView) {
        super(itemView);
        initItemView(itemView);
    }

    /**
     * 初始化item，在此方法中对view进行绑定，可以findViewById、ButterKnife注入等
     * <br/>原始方法设置了点击事件onItemClick
     *
     * @param itemView item view
     */
    protected void initItemView(View itemView) {
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int itemPosition = getAdapterPosition();
                if (mOnItemClickListener != null && itemPosition != -1) {
                    mOnItemClickListener.onItemClick(v, itemPosition);
                }
            }
        });
    }

    /**
     * 在onBindViewHolder中调用，在此方法中将数据绑定到各view上
     * 可以DataBind
     *
     * @param bean bean
     * @param position 位置，来自于{@linkplain BaseRecyclerViewAdapter#onBindViewHolder(BaseRecyclerViewHolder, int)}
     */
    public void bindTo(Bean bean, int position) {
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    /**
     * 点击某一条item
     */
    public interface OnItemClickListener {

        /**
         * 点击了item
         *
         * @param itemView 点击的item view
         * @param position 位置，来自于getAdapterPosition()
         */
        void onItemClick(View itemView, int position);
    }
}
